﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace PeisongWeb.admin
{
    public partial class reportProductCategory : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("EC", 1))
                {
                    drpBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }

            }
        }
        private void drpBind()
        {
            drp_CategoryRoot.DataSource = logic.category.getByParent(0);
            drp_CategoryRoot.DataBind();
            drp_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));

            drp_Company.DataSource = logic.company.select("type<>3","companyid",1000,1).Tables[1];
            drp_Company.DataBind();
            drp_Company.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", "0"));
        }

        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1 ");
            if (drp_CategoryRoot.SelectedValue.ToString().Trim() != "")
                where.AppendFormat("and parentId={0}",drp_CategoryRoot.SelectedValue.ToString().Trim());
            if (drp_CategorySub.SelectedValue.ToString().Trim() != "")
                where.AppendFormat("and categoryId={0}", drp_CategorySub.SelectedValue.ToString().Trim());
            if (ext_StartInputDt.Value.ToString() != "0001-1-1 0:00:00")
                where.AppendFormat(" and datediff(day,[inputDt],'{0}')<=0", ext_StartInputDt.Value.ToString());
            if (ext_EndInputDt.Value.ToString() != "0001-1-1 0:00:00")
                where.AppendFormat(" and datediff(day,[inputDt],'{0}')>=0", ext_EndInputDt.Value.ToString());
            int companyId = Convert.ToInt32(drp_Company.SelectedItem.Value);
            string type = logic.company.getById(companyId).Type;
            if (companyId!=0)
            {
                if (type=="2")
                    where.AppendFormat("and companyId={0}",companyId);
                else
                    where.AppendFormat("and sellerId={0}", companyId);
            }
            DataTable dt = logic.category.listWithReport(where.ToString());
            dt.Columns.Add("Type",typeof(Int32));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (type == "2"||companyId==0)
                {
                    dt.Rows[i]["type"] = 2;
                    lbl_Price.InnerText = "平均单价";
                }
                else
                {
                    dt.Rows[i]["type"] = 1;
                    lbl_Price.InnerText = "供应商平均价格";
                }
            }

            ////临时表，存储dt相同的row
            //DataTable dts = dt.Clone();

            //foreach (System.Data.DataRow dr in dt.Rows)//取goodsdanid
            //{
            //    DataRow dryu;
            //    dts.Clear();
            //    DataRow[] drs = dt.Select("parentId='" + dr["parentId"] + "'");
            //    foreach (System.Data.DataRow dra in drs)
            //    {
            //        dryu = dts.NewRow();
            //        dryu.ItemArray = dra.ItemArray;
            //        dts.Rows.Add(dryu);
            //    }
            //    double n = double.Parse(dts.Compute("sum(sumQuantity)", "1=1").ToString());
            //    //将结果加到剩余表  
            //    dryu = dt.NewRow();
            //    dryu.ItemArray = dts.Rows[0].ItemArray;

            //    dryu[6] = dts.Compute("sum(sumQuantity)", "1=1");
            //    dt.Rows.Add(dryu);
            //}

            rpt_list.DataSource = dt;
            rpt_list.DataBind();
           
            
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "details")
            {
                hid_categoryId.Value = e.CommandArgument.ToString();
                rptDetailsBind();
                category_details.Show();
                txt_orderNo.Text = "";
                txt_startOrderDt.Clear();
                txt_endOrderDt.Clear();
            }
        }
        void categorySubBind()
        {
            if (drp_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.category.getByParent(Convert.ToInt32(drp_CategoryRoot.SelectedValue));
                drp_CategorySub.DataSource = dt;
                drp_CategorySub.DataBind();
                drp_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
            else
            {
                drp_CategorySub.Items.Clear();
            }
        }
        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            categorySubBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }


        void rptDetailsBind()
        {
            int categoryid = Convert.ToInt32(hid_categoryId.Value);
            StringBuilder where = new StringBuilder();
            where.Append("categoryId="+categoryid);
            if (txt_orderNo.Text.ToString().Trim() != "")
                where.AppendFormat("and orderno = '{0}'", txt_orderNo.Text.ToString().Trim());
            if (txt_startOrderDt.Value.ToString() != "0001-1-1 0:00:00")
                where.AppendFormat(" and datediff(day,[inputDt],'{0}')<=0", txt_startOrderDt.Value.ToString());
            if (txt_endOrderDt.Value.ToString() != "0001-1-1 0:00:00")
                where.AppendFormat(" and datediff(day,[inputDt],'{0}')>=0", txt_endOrderDt.Value.ToString());
            DataSet ds = logic.orderItem.listByCategory(where.ToString() ,"oiid", AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            AspNetPager2.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_Products.DataSource = ds.Tables[1];
            rpt_Products.DataBind();
            category_details.Title = ds.Tables[1].Rows[0]["subCategory"].ToString()+"的详情信息";

        }
       protected void lbn_search_Click(object sender, EventArgs e)
       {
           rptDetailsBind();
       }
       protected void AspNetPager2_PageChanged(object sender, EventArgs e)
       {
           rptDetailsBind();
       }
    }
}
